import Vue from 'vue'
import VueRouter from 'vue-router'
import Home from '../views/Home.vue'

Vue.use(VueRouter)

const routes = [
        {
                path: '/',
                name: 'Home',
                component: Home,
                meta: {
                        isLogin: false,
                        keep: true
                }
        },
        {
                path: '/user',
                name: 'Name',
                component: () => import('../views/User.vue'),
                meta: {
                        isLogin: true,
                        keep: true
                }
        },
        {
                path: '/login',
                name: 'Login',
                component: () => import('../views/Login.vue'),
                meta: {
                        isLogin: false,
                        keep: false
                }
        },
        {
                path: '/register',
                name: 'Register',
                component: () => import('../views/Register.vue'),
                meta: {
                        isLogin: false,
                        keep: false
                }
        },
        {
                path: '/write',
                name: 'Write',
                component: () => import('../views/Write.vue'),
                meta: {
                        isLogin: true,
                        keep: true
                }
        },
        {
                path: '/discover',
                name: 'discover',
                component: () => import('../views/Discover.vue'),
                meta: {
                        isLogin: true,
                        keep: true
                }
        },
        {
                path: '/hottopic',
                name: 'hottopic',
                component: () => import('../views/HotTopic.vue'),
                meta: {
                        isLogin: true,
                        keep: true
                }
        },
        {
                path: '/specialColumn',
                name: 'specialColumn',
                component: () => import('../views/SpecialColumn.vue'),
                meta: {
                        isLogin: true,
                        keep: true
                }
        },
        {
                path: '/perSignatue',
                name: 'perSignatue',
                component: () => import('../views/PerSignatue.vue'),
                meta: {
                        isLogin: true,
                        keep: true
                }
        },
        {
            path: '/essay/:id',
            name: 'essay',
            component: () => import('../views/Essay.vue'),
            meta: {
                    isLogin: true,
                    keep: true
            }
    },
]

const router = new VueRouter({
        routes
})

router.beforeEach((to, from, next) => {
        if (to.meta.isLogin) {
                if (localStorage.getItem("token")) {
                        next();
                } else {
                        next({
                                path: '/login',
                                query: { redirect: to.fullPath }
                        })
                }
        } else {
                next();
        }
})
export default router
